package liuliu.throughwaring.ui.activity;

import android.app.Dialog;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.login.ILoginView;
import liuliu.throughwaring.control.login.LoginListener;
import liuliu.throughwaring.control.task.QueryTask;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.Key;
import liuliu.throughwaring.model.PoliceModel;

/**
 * Created by liuliu on 2015/11/13   14:41
 * <p/>
 * 登录
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class LoginActivity extends BaseActivity implements ILoginView {
    public static LoginActivity mInstance;
    @CodeNote(id = R.id.login_choice_police_et, click = "onClick")
    EditText choice_police;
    @CodeNote(id = R.id.login_password_et)
    EditText password;
    @CodeNote(id = R.id.login_button, click = "onClick")
    Button login_btn;
    @CodeNote(id = R.id.login_right_menu_draw)
    DrawerLayout right_menu;//右侧菜单
    @CodeNote(id = R.id.right_menu_toolbar)
    TToolbar toolbar;
    @CodeNote(id = R.id.right_menu_listview)
    ListView mRightMenu;
    List<PoliceModel> mPoliceList;
    PoliceModel model;
    LoginListener mListener;
    int clickPosition;//左划菜单点击的位置
    String txt_userid;
    String txt_pwd;
    Dialog dialog;
    @CodeNote(id = R.id.txt_xieti)
    TextView txt;
    SpannableString msp = null;//字斜体设置
    @CodeNote(id = R.id.login_checkbox)
    CheckBox isLogin;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_login);
        msp = new SpannableString("POLICE");
        msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //斜体
        txt.setText(msp);
        mUtils = new Utils(this);
        mInstance = this;
        initDatas();
        mListener = new LoginListener(this, this, finalDb);
    }

    //加载数据
    private void initDatas() {
        mPoliceList = finalDb.findAll(PoliceModel.class);
        String pid = Utils.ReadString("PoliceId");
        if (Utils.ReadString("LoginUser").equals("1")) {
            if (!pid.equals("")) {
                for (PoliceModel p : mPoliceList) {
                    if (p.getPoliceId().equals(pid)) {
                        choice_police.setText(p.getPoliceName());
                        isLogin.setChecked(true);
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void initEvents() {
        //左划菜单返回按钮点击事件
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                drawerManager();
            }
        });
        mRightMenu.setVisibility(View.VISIBLE);
        //左划菜单的Adapter
        mRightMenu.setAdapter(new CommonAdapter<PoliceModel>(mInstance, mPoliceList,
                R.layout.right_menu_item_police) {
            @Override
            public void convert(final ViewHolder holder, final PoliceModel model, int position) {
                holder.setText(R.id.right_menu_policename, model.getPoliceName());
                holder.setText(R.id.right_menu_policeid, model.getPoliceId());
            }
        });
        //左划菜单项点击事件
        mRightMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                drawerManager();
                clickPosition = position;
                choice_police.setText(mPoliceList.get(position).getPoliceName());
                Utils.WriteString("PoliceId", mPoliceList.get(position).getPoliceId());
            }
        });
        Utils.setListViewHeightBasedOnChildren(this, mRightMenu);
    }

    /*开启关闭左划菜单*/
    private void drawerManager() {
        if (right_menu.isDrawerOpen(GravityCompat.END)) {
            right_menu.closeDrawer(GravityCompat.END);
        } else {
            right_menu.openDrawer(GravityCompat.END);
            toolbar.setCenterTitle("选择民警");
        }
    }

    /*点击事件*/
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_button:
                //用户和密码不能为空
                txt_userid = choice_police.getText().toString();
                txt_pwd = password.getText().toString().trim();
                if (txt_userid.equals("请选择民警")) {
                    mUtils.ToastShort("请选择登录的民警");
                } else if (txt_pwd.equals("")) {
                    mUtils.ToastShort("密码不能为空！");
                } else {
                    dialog = Utils.ProgressDialog(LoginActivity.this, dialog, "登录中，请稍候...", false);
                    dialog.show();

                    mListener.login(txt_pwd);
                }
                break;
            case R.id.login_choice_police_et://打开抽屉
                right_menu.openDrawer(GravityCompat.END);
                toolbar.setCenterTitle("选择民警");
                break;
        }
    }

    @Override
    public void OnLoginResult(boolean result, String message) {
        if (dialog != null) {
            dialog.dismiss();
        }
        if (!message.equals("")) {
            mUtils.ToastShort(message);
        }
        if (result) {
            if (isLogin.isChecked()) {
                Utils.WriteString("LoginUser", "1");
            } else {
                Utils.WriteString("LoginUser", "0");
            }
            //上传民警绑定设备
            mListener.updatePolice();
        }
    }

    @Override
    public void UpdatePolice(boolean result, String mes) {
        if (result) {
            mInstance.finish();
            List<CodeModel> codeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_Area'");
            List<PoliceModel> police = finalDb.findAllByWhere(PoliceModel.class, "PoliceId='" + Utils.ReadString("PoliceId") + "'");
            if (police.size() > 0) {
                Utils.WriteString(Key.KEY_POLICE_NAME, police.get(0).getPoliceName());
                Utils.WriteString(Key.KEY_STATION_NAME, Utils.getCodeVlaue(codeList, police.get(0).getPoliceArea()));
            }
            mUtils.IntentPost(MainActivity.class);//跳转设置
            Utils.WriteString("Longitud", "0.0");
            Utils.WriteString("Latitude", "0.0");
            QueryTask tak = new QueryTask(this);
            tak.getNationAlarm(null);
        } else {
            mUtils.ToastShort(mes);
        }
    }
}
